﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Order_AddSuggestion : System.Web.UI.Page
{
    private string Code
    {
        get
        {
            return Request.QueryString["Code"] != null ? Request.QueryString["Code"].ToString() : "";
        }
    }

    private string OrderStateTypeCode
    {
        get
        {
            return Request.QueryString["stCode"] != null ? Request.QueryString["stCode"].ToString() : "";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("400101");
        if (!IsPostBack)
        {
            BindDropSuggestionType();
        }
    }

    private void BindDropSuggestionType()
    {
        dropSuggestionType.DataSource = new BLL.STATETYPE().GetAllList();
        dropSuggestionType.DataTextField = "ST_NAME";
        dropSuggestionType.DataValueField = "ST_CODE";
        dropSuggestionType.DataBind();

        for (int i = 0; i < dropSuggestionType.Items.Count; i++)
        {
            if (dropSuggestionType.Items[i].Value == this.OrderStateTypeCode)
            {
                dropSuggestionType.SelectedIndex = i;
                //dropSuggestionType.Enabled = false;
                break;
            }
        }
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["ts"] != null)
        {
            if (!Check.CheckPermission("400102", "00021"))
            {
                Share.Alert(this.Page, "无权操作"); return;
            }
            if (dropSuggestionType.Items.Count > 0)
            {
                Model.RETURNNOTE rn = new Model.RETURNNOTE();
                rn.RN_RETURNID = this.Code;
                rn.RN_USERCODE = Share.UserValue(0);
                rn.RN_TIME = DateTime.Now;
                rn.RN_NOTETYPE = dropSuggestionType.SelectedItem.Value;
                rn.RN_NOTE = txtSuggestion.Text.Trim();

                if (new BLL.RETURNNOTE().Add(rn) > 0)
                {
                    Share.InsertLog("400102", "00021");
                    Response.Write("<script>alert('添加意见成功！');location.href='ReturnDocEdit.aspx?Code=" + this.Code + "'</script>");
                }
                else
                {
                    Share.Alert(this.Page, "提交意见时出现错误，请重新添加！");
                }
            }
        }
        else
        {
            if (!Check.CheckPermission("400101", "00021"))
            {
                Share.Alert(this.Page, "无权操作"); return;
            }
            if (dropSuggestionType.Items.Count > 0)
            {
                Model.ORDERNOTE suggestion = new Model.ORDERNOTE();
                suggestion.ON_BILLNO = this.Code;
                suggestion.ON_USERCODE = Share.UserValue(0);
                suggestion.ON_TIME = DateTime.Now;
                suggestion.ON_NOTETYPE = dropSuggestionType.SelectedItem.Value;
                suggestion.ON_NOTE = txtSuggestion.Text.Trim();

                Model.ORDERDOC order = new BLL.ORDERDOC().GetModel(this.Code);
                if (order != null)
                {
                    order.ORDER_STATETYPECODE = dropSuggestionType.SelectedItem.Value;
                    new BLL.ORDERDOC().Update(order);
                }


                if (new BLL.ORDERNOTE().Add(suggestion) > 0)
                {
                    Share.InsertLog("400101", "00021");
                    Response.Write("<script>alert('添加意见成功！');location.href='OrderDetail.aspx?Code=" + this.Code + "'</script>");
                }
                else
                {
                    Share.Alert(this.Page, "提交意见时出现错误，请重新添加！");
                }
            }
        }
    }
}
